package wangm.netty.test1;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONReader;
import wangm.bean.User;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.io.*;
import java.util.List;

/**
 * description
 *
 * @author zk
 * @since 2018/7/20
 */
public class Test1ByteToMessageDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        String jsonString = new String(ByteBufUtil.getBytes(in),"UTF-8");
        User user = JSON.toJavaObject(JSONObject.parseObject(jsonString),User.class);
        out.add(user);
    }
}
